<template>
  <div class="question-mode-selection">
    <div class="page-header">
      <button class="back-btn" @click="handleGoBack">
        <span class="back-icon">←</span>
        返回
      </button>
      <h1 class="page-title">✍️ 答题设置</h1>
      <p class="page-subtitle">学习方向：{{ this.$route.params.categoryName }} / {{ this.$route.params.subCategoryName }}</p>
    </div>

    <!-- 智能推荐选项 -->
    <div class="quick-start-section">
      <div class="section-header">
        <h2>📖 答题方式</h2>
        <p>选择你的答题方式</p>
      </div>
      <div class="quick-start-options">
        <div class="quick-start-card" @click="startQuickLearning">
          <div class="quick-start-icon">🧠</div>
          <h3 class="quick-start-title">智能推荐</h3>
          <p class="quick-start-description">根据用户答题情况，动态推荐题目</p>
        </div>
        <div class="quick-start-card" @click="showDetailedSettings = true">
          <div class="quick-start-icon">📚</div>
          <h3 class="quick-start-title">自主答题</h3>
          <p class="quick-start-description">自定义学习模式、题型和数量</p>
        </div>
      </div>
    </div>

    <!-- 详细学习设置 -->
    <div v-if="showDetailedSettings" class="detailed-settings">
      <!-- 学习模式选择 -->
      <div class="modes-section">
        <div class="section-header">
          <h2>🎯 学习模式</h2>
          <p>选择适合你的学习方式</p>
        </div>
        <div class="modes-grid">
          <div
            v-for="mode in modes"
            :key="mode.value"
            class="mode-card"
            :class="{ selected: selectedMode === mode.code }"
            @click="selectMode(mode.code)"
          >
            <div class="mode-icon">{{ mode.icon }}</div>
            <h3 class="mode-title">{{ mode.name }}</h3>
            <p class="mode-description">{{ mode.description }}</p>
            <div class="mode-features">
              <span class="feature-tag">{{ mode.features?.[0] || '自由练习' }}</span>
              <span class="feature-tag">{{ mode.features?.[1] || '即时反馈' }}</span>
            </div>
          </div>
        </div>
      </div>

      <!-- 题型选择 -->
      <div class="types-section">
        <div class="section-header">
          <h2>📝 题型选择</h2>
          <p>选择你想要的题目类型</p>
        </div>
        <div class="types-grid">
          <div
            v-for="type in types"
            :key="type.value"
            class="type-card"
            :class="{ selected: selectedType === type.code }"
            @click="selectType(type.code)"
          >
            <div class="type-icon">{{ type.icon }}</div>
            <h3 class="type-title">{{ type.name }}</h3>
            <p class="type-description">{{ type.description }}</p>
          </div>
        </div>
      </div>

      <!-- 题目数量选择 -->
      <div class="limit-section">
        <div class="section-header">
          <h2>🔢 题目数量</h2>
          <p>选择你想要的题目数量</p>
        </div>
        <div class="limit-options">
          <div
            v-for="limit in limits"
            :key="limit.value"
            class="limit-option"
            :class="{ selected: selectedLimit === limit.code }"
            @click="selectLimit(limit.code)"
          >
            <div class="limit-number">{{ limit.label }}</div>
            <div class="limit-label">{{ limit.description }}</div>
          </div>
        </div>
      </div>

      <!-- 开始学习按钮 -->
      <div class="start-section">
        <el-button
          type="primary"
          size="large"
          :disabled="!selectedMode || loading"
          :loading="loading"
          @click="startLearning"
          class="start-btn"
        >
          <span v-if="!loading" class="btn-icon">🎯</span>
          <span v-else class="loading-icon">⏳</span>
          {{ loading ? '获取题目中...' : '开始学习' }}
        </el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { getStudyModesList } from '@/api/studyModes'
import { getQuestionTypesList } from '@/api/questionTypes'

export default {
  name: "QuestionModeSelection",
  props: {
    loading: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      showDetailedSettings: false, // 是否显示详细设置
      selectedMode: "", // 选中的学习模式
      selectedType: "all", // 选中的题型
      selectedLimit: "10", // 选中的题目数量
      // 从路由参数获取分类信息
      categoryId: this.$route.params.categoryId,
      subCategoryId: this.$route.params.subCategoryId,
      modes: [], // 从后端获取的学习模式列表
      types: [], // 从后端获取的题型列表
      limits: [
        {
          id: "10",
          label: "10题",
          value: "10",
          code: "10",
          icon: "🔢",
          description: "快速练习，适合快速掌握",
        },
        {
          id: "20",
          label: "20题",
          value: "20",
          code: "20",
          icon: "🔢",
          description: "中等练习，适合巩固知识点",
        },
        {
          id: "50",
          label: "50题",
          value: "50",
          code: "50",
          icon: "🔢",
          description: "大量练习，适合深入学习",
        },
      ]
    }
  },
  methods: {
    // 获取学习模式列表
    async fetchStudyModes() {
      try {
        const response = await getStudyModesList();
        if (response && response.data) {
          this.modes = response.data;
        } else {
          // 如果后端接口返回的数据结构不同，可以在这里调整
          this.modes = response || [];
        }
      } catch (error) {
        console.error('获取学习模式列表失败:', error);
        // 接口调用失败时，直接提示错误信息
        this.$message.error('获取学习模式列表失败，请稍后重试');
        throw error; // 重新抛出错误，让调用方知道失败了
      }
    },

    // 获取题型列表
    async fetchQuestionTypes() {
      try {
        const response = await getQuestionTypesList();
        if (response && response.data) {
          this.types = response.data;
        } else {
          // 如果后端接口返回的数据结构不同，可以在这里调整
          this.types = response || [];
        }
      } catch (error) {
        console.error('获取题型列表失败:', error);
        // 接口调用失败时，直接提示错误信息
        this.$message.error('获取题型列表失败，请稍后重试');
        throw error; // 重新抛出错误，让调用方知道失败了
      }
    },

    selectMode(mode) {
      this.selectedMode = mode;
    },

    selectType(type) {
      this.selectedType = type;
    },

    selectLimit(limit) {
      this.selectedLimit = limit;
    },

    // 智能推荐学习（使用推荐设置）
    startQuickLearning() {
      // 检查数据是否已加载
      if (this.modes.length === 0 || this.types.length === 0) {
        this.$message.error('学习数据加载中，请稍后再试');
        return;
      }
      console.log('startQuickLearning',this.$route.params)
      // 跳转到智能推荐答题组件
      this.$router.push({
        path: '/smart-recommend',
        query: {
          categoryId: this.$route.params.categoryId,
          categoryName: this.$route.params.categoryName,
          subCategoryId: this.$route.params.subCategoryId,
          subCategoryName: this.$route.params.subCategoryName
        }
      });
    },

    startLearning() {
      if (!this.selectedMode) {
        this.$message.warning('请先选择学习模式');
        return;
      }

      // 检查数据是否已加载
      if (this.modes.length === 0 || this.types.length === 0) {
        this.$message.error('学习数据加载中，请稍后再试');
        return;
      }

      // 找到选中的模式和题型对象
      const selectedModeObj = this.modes.find(m => m.code === this.selectedMode);
      const selectedTypeObj = this.types.find(t => t.code === this.selectedType);

      this.$emit('start-learning', {
        studyModeId: selectedModeObj?.id || this.selectedMode, // 改为studyModeId
        questionTypeId: selectedTypeObj?.id || this.selectedType,  // 改为questionTypeId
        limit: this.selectedLimit, // 添加题目数量
        mode: "detailed" // 标识为详细设置模式
      });
    },

    // 初始化数据
    async init() {
      try {
        await Promise.all([
          this.fetchStudyModes(),
          this.fetchQuestionTypes()
        ]);
      } catch (error) {
        console.error('初始化数据失败:', error);
        // 如果初始化失败，可以在这里添加额外的错误处理逻辑
        // 比如显示重试按钮或者禁用某些功能
      }
    },

    handleGoBack() {
      // 返回到模式选择页面
      this.$router.push({
        name: 'learn-mode',
        params: {
          categoryId: this.categoryId,
          subCategoryId: this.subCategoryId
        }
      });
    }
  },

  async mounted() {
    await this.init();
  }
}
</script>

<style scoped>
/* 答题模式选择页面 */
.question-mode-selection {
  max-width: 1000px;
  margin: 0 auto;
}

/* 页面头部样式 */
.page-header {
  text-align: center;
  margin-bottom: 30px;
  padding: 30px 0;
}

.page-title {
  font-size: 2rem;
  font-weight: 700;
  color: #2c3e50;
  margin: 0 0 10px 0;
}

.page-subtitle {
  font-size: 1rem;
  color: #7f8c8d;
  margin: 0;
}

/* 智能推荐区域 */
.quick-start-section {
  margin-bottom: 40px;
}

.quick-start-options {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
  margin-top: 20px;
}

.quick-start-card {
  background: white;
  border-radius: 20px;
  padding: 30px;
  text-align: center;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
  border: 2px solid transparent;
  transition: all 0.3s ease;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.quick-start-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.15);
  border-color: #3b82f6;
}

.quick-start-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  transform: scaleX(0);
  transition: transform 0.3s ease;
}

.quick-start-card:hover::before {
  transform: scaleX(1);
}

.quick-start-icon {
  font-size: 3rem;
  margin-bottom: 15px;
  display: block;
}

.quick-start-title {
  font-size: 1.3rem;
  font-weight: 700;
  color: #2c3e50;
  margin: 0 0 10px 0;
}

.quick-start-description {
  font-size: 1rem;
  color: #7f8c8d;
  line-height: 1.5;
  margin: 0;
}

/* 详细设置区域 */
.detailed-settings {
  animation: slideDown 0.5s ease-out;
}

@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 区域通用样式 */
.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 25px;
  flex-wrap: wrap;
  gap: 10px;
}

.section-header h2 {
  font-size: 1.5rem;
  font-weight: 700;
  color: #2c3e50;
  margin: 0;
}

.section-header p {
  font-size: 0.9rem;
  color: #7f8c8d;
  margin: 0;
  flex: 1;
  text-align: center;
}

.modes-section {
  margin-bottom: 30px;
}

.types-section {
  margin-bottom: 30px;
}

.limit-section {
  margin-bottom: 30px;
}

.modes-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
}

.types-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
}

.limit-options {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(80px, 1fr));
  gap: 10px;
  justify-items: center;
}

.limit-option {
  background: white;
  border-radius: 15px;
  padding: 15px;
  text-align: center;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.limit-option:hover {
  transform: translateY(-3px);
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
}

.limit-option.selected {
  border-color: #3b82f6;
  background: linear-gradient(135deg, rgba(59, 130, 246, 0.1) 0%, rgba(59, 130, 246, 0.05) 100%);
  box-shadow: 0 6px 15px rgba(59, 130, 246, 0.3);
}

.limit-number {
  font-size: 1.4rem;
  font-weight: 700;
  color: #2c3e50;
}

.limit-label {
  font-size: 0.8rem;
  color: #7f8c8d;
}

.mode-card {
  background: white;
  border-radius: 15px;
  padding: 15px;
  text-align: center;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
  animation: fadeInUp 0.6s ease-out forwards;
  opacity: 0;
  transform: translateY(30px);
  cursor: pointer;
}

.mode-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
}

.mode-card.selected {
  border-color: #3b82f6;
  background: linear-gradient(135deg, rgba(59, 130, 246, 0.1) 0%, rgba(59, 130, 246, 0.05) 100%);
  box-shadow: 0 6px 15px rgba(59, 130, 246, 0.3);
}

@keyframes fadeInUp {
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.mode-card:nth-child(1) { animation-delay: 0.1s; }
.mode-card:nth-child(2) { animation-delay: 0.2s; }
.mode-card:nth-child(3) { animation-delay: 0.3s; }

.mode-icon {
  font-size: 1.8rem;
  margin-bottom: 10px;
  display: block;
}

.mode-title {
  font-size: 0.9rem;
  color: #2c3e50;
  margin: 0 0 8px 0;
  font-weight: 600;
}

.mode-description {
  font-size: 0.85rem;
  color: #7f8c8d;
  line-height: 1.4;
  margin: 0 0 15px 0;
}

.mode-features {
  display: flex;
  justify-content: center;
  gap: 8px;
  flex-wrap: wrap;
}

.feature-tag {
  padding: 4px 8px;
  background: rgba(59, 130, 246, 0.1);
  color: #3b82f6;
  border-radius: 10px;
  font-size: 0.7rem;
  font-weight: 500;
}

.type-card {
  background: white;
  border-radius: 15px;
  padding: 15px;
  text-align: center;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
  animation: fadeInUp 0.6s ease-out forwards;
  opacity: 0;
  transform: translateY(30px);
  cursor: pointer;
}

.type-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
}

.type-card.selected {
  border-color: #3b82f6;
  background: linear-gradient(135deg, rgba(59, 130, 246, 0.1) 0%, rgba(59, 130, 246, 0.05) 100%);
  box-shadow: 0 6px 15px rgba(59, 130, 246, 0.3);
}

.type-card:nth-child(1) { animation-delay: 0.1s; }
.type-card:nth-child(2) { animation-delay: 0.2s; }
.type-card:nth-child(3) { animation-delay: 0.3s; }
.type-card:nth-child(4) { animation-delay: 0.4s; }
.type-card:nth-child(5) { animation-delay: 0.5s; }

.type-icon {
  font-size: 1.8rem;
  margin-bottom: 10px;
  display: block;
}

.type-title {
  font-size: 0.9rem;
  color: #2c3e50;
  margin: 0 0 8px 0;
  font-weight: 600;
}

.type-description {
  font-size: 0.75rem;
  color: #7f8c8d;
  line-height: 1.4;
  margin: 0;
}

.start-section {
  text-align: center;
  margin-top: 30px;
  padding: 30px 0;
}

.start-btn {
  padding: 15px 40px;
  border: none;
  border-radius: 25px;
  font-size: 1.1rem;
  font-weight: 600;
  background: linear-gradient(135deg, #1e3a8a 0%, #3b82f6 100%);
  color: white;
  cursor: pointer;
  transition: all 0.3s ease;
  display: inline-flex;
  align-items: center;
  gap: 10px;
  box-shadow: 0 6px 20px rgba(59, 130, 246, 0.3);
}

.start-btn:hover:not(.disabled) {
  transform: translateY(-2px);
  box-shadow: 0 10px 25px rgba(30, 58, 138, 0.4);
}

.start-btn.disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.btn-icon {
  font-size: 1rem;
}

.loading-icon {
  font-size: 1rem;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.back-btn {
  padding: 10px 20px;
  border: 2px solid #e9ecef;
  border-radius: 20px;
  font-size: 0.9rem;
  color: #3b82f6;
  background: white;
  cursor: pointer;
  transition: all 0.3s ease;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-weight: 600;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1);
}

.back-btn:hover {
  background: #3b82f6;
  color: white;
  border-color: #3b82f6;
  transform: translateY(-2px);
  box-shadow: 0 6px 15px rgba(59, 130, 246, 0.3);
}

.back-icon {
  font-size: 1rem;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .page-title {
    font-size: 1.6rem;
  }

  .page-subtitle {
    font-size: 0.9rem;
  }

  .section-header {
    flex-direction: column;
    text-align: center;
  }

  .section-header h2 {
    font-size: 1.3rem;
  }

  .quick-start-options {
    grid-template-columns: 1fr;
  }

  .modes-grid {
    grid-template-columns: 1fr;
  }

  .types-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .limit-options {
    grid-template-columns: repeat(3, 1fr);
  }

  .start-btn {
    padding: 12px 30px;
    font-size: 1rem;
  }
}

@media (max-width: 480px) {
  .page-title {
    font-size: 1.4rem;
  }

  .page-subtitle {
    font-size: 0.8rem;
  }

  .quick-start-card {
    padding: 20px;
  }

  .quick-start-icon {
    font-size: 2.5rem;
  }

  .quick-start-title {
    font-size: 1.1rem;
  }

  .types-grid {
    grid-template-columns: 1fr;
  }

  .limit-options {
    grid-template-columns: repeat(2, 1fr);
  }

  .mode-card,
  .type-card {
    padding: 15px;
  }

  .start-btn {
    padding: 10px 25px;
    font-size: 0.9rem;
  }
}
</style>
